//package com.woniuxy.education.configuration.realm;
//
//import com.woniuxy.education.entity.Perm;
//import com.woniuxy.education.entity.Role;
//import com.woniuxy.education.entity.User;
//import com.woniuxy.education.service.UserService;
//import com.woniuxy.education.utils.R;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
//
////AuthorizingRealm 授权 realm
//public class CustomRealm extends AuthorizingRealm {
//    @Autowired
//    private UserService userService;//注入Service对象
//    /*
//    获取权限信息
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
////        String account=(String) principals.getPrimaryPrincipal();
////        Set<String> roles = new HashSet<>();
////        Set<String> perms= new HashSet<>();
////        //以账号作为查询条件,查询出当前账号的所有角色,权限管理系统中一般都存在一个账号多个角色
////        User user = userService.findUserAndRolesByAccont(account);
////        //遍历获取角色和权限信息
////        for (Role role: user.getRoles()) {
////            roles.add(role.getName());
////            for(Perm perm : role.getPerms()){
////                perms.add((perm.getMenuname()));
////            }
////        }
////        System.out.println(roles);
////        //封装角色信息
////        SimpleAuthorizationInfo info =
////                new SimpleAuthorizationInfo(roles);
////        //封装权限信息
////        info.setStringPermissions(perms);
////        return info;//返回给securitymanger 也就是 (subject)
//        return null;
//    }
//
//    /*
//    获取认证信息
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        //token中包含了账号密码信息
//        //获取账号密码信息
//        String account=(String)token.getPrincipal();
//        String password=new String((char[])token.getCredentials());
//        //通过账号作为查询条件,去数据库中查出对应的账号密码信息  调用service
//        //模拟假设获得了账号密码
//        User user = userService.findUserByAccont(account);
//
//        if (user==null){
//            return null;//账号不存在异常
//        }
//        //将查询到的结果封装到AuthenticationInfo返回给securitymagger
//        //securitymagger会根据AuthenticationInfo中的信息与totken中的信息做比对  ,判断账号密码是否正确
//        //参数1:查询到的账号
//        //参数2:查询到的密码
//        //参数3.当前realm的名字
//        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getAccount(),user.getPassword(),this.getName());
//        List<Perm> listAllPerms=userService.queryAllMenu(user.getId());
//        SecurityUtils.getSubject().getSession().setAttribute("userperms",listAllPerms);
//        SecurityUtils.getSubject().getSession().setAttribute("user",user);
//        return  info;
//    }
//
//
//}
